Summary

When providing the same input parameters and units, class and classy has the same outputs.

In [1]:
import os
import io
from pathlib import Path
In [2]:
import pandas as pd
import numpy as np
/home/kolen/.conda/envs/all3-intel/lib/python3.6/importlib/_bootstrap.py:219: RuntimeWarning: numpy.ufunc size changed, may indicate binary incompatibility. Expected 216, got 192
  return f(*args, **kwds)
In [3]:
import matplotlib.pyplot as plt
In [4]:
%matplotlib inline
In [5]:
import plotly.offline as py
py.init_notebook_mode()
In [6]:
import holoviews as hv
hv.extension('plotly')
In [7]:
IDX = pd.IndexSlice
In [8]:
%load_ext autoreload
%autoreload 2
In [9]:
from dautil.plot import iplot_column_slider, plot_column_slider
In [10]:
def read_txt(path):
    with open(path, 'r') as f:
        # remove beginning `#`
        text = f.read()[1:]
    with io.StringIO(text) as f:
        # input has 6 sig. fig. which fits in float32
        return pd.read_csv(f, delim_whitespace=True, index_col=0, dtype=np.float32)
In [11]:
def read_class_txt(path, camb=False):
    # read once
    with open(path, 'r') as f:
        text = f.read()

    # get last comment line
    comment = None
    for line in text.split('\n'):
        if line.startswith('#'):
            comment = line
    # remove beginning '#'
    comment = comment[1:]
    # parse comment line: get name after ':'
    names = [name.split(':')[1] for name in comment.strip().split()]

    with io.StringIO(text) as f:
        df = pd.read_csv(f, delim_whitespace=True, index_col=0, comment='#', header=None, names=names)
    df.columns.name = 'spectra'
    return df if camb else df * 1.e12
In [12]:
def plot_compare(df1, df2, keys, log=False, relative=False):
    '''plot to compare columns from `df1` and `df2`,
    using columns from `df1`.

    `keys`: tuple of str of names of `df1` and `df2`.
    `relative`: if True, plot the relative error w.r.t. `df1` instead.
    '''
    for col in df1.columns:
#         pd.merge(df1[col], df2[col], left_index=True, right_index=True).plot()
        df_temp = pd.concat((df1[col], df2[col]), axis=1, join='inner', keys=(' '.join((key, col)) for key in keys))
        if relative:
            temp = df_temp.values
            if log:
                plt.loglog(df_temp.index, np.abs((temp[:, 0] - temp[:, 1]) / temp[:, 0]), label=col)
            else:
                plt.plot(df_temp.index, np.abs((temp[:, 0] - temp[:, 1]) / temp[:, 0]), label=col)
            plt.legend()
            plt.show()
        else:
            if log:
                df_temp.plot(logx=True, logy=True)
            else:
                df_temp.plot()
In [13]:
# first value is default
# rest are alternative value for each realization
PARAMS = {
    'STELLAR_BARYON_FRAC': (0.05, 0.001, 1.),
    'STELLAR_BARYON_PL': (0.5, -0.5, 1.),
    'ESC_FRAC': (0.01, 0.001, 1.),
    'ESC_PL': (-0.5, -1., 0.5),
    'M_TURNOVER': (5e8, 1e8, 1e10),
    't_STAR': (0.5, 0., 1.),
    'L_X': (40.5, 38., 42.)
}
In [14]:
def parse_case(string):
    for level, idx in enumerate(map(int, string.split('_'))):
        if idx != 0:
            break
    if idx == 0:
        return 'default'
    else:
        param = list(PARAMS)[level]
        return '{}={:.6}'.format(param, PARAMS[param][idx])
In [15]:
basedir = Path('~/git/source/UCB-PHYS229-2019Spring-final-project/output').expanduser()
In [16]:
df_path = pd.DataFrame(basedir.glob('*-cl_lensed.dat'), columns=['path'])
In [17]:
df_path['case'] = df_path.path.map(lambda path: path.name.split('-')[0])
In [18]:
df_path['name'] = df_path.case.map(parse_case)
In [19]:
df_path
Out[19]:
path case name
0 /home/kolen/git/source/UCB-PHYS229-2019Spring-... 0_0_0_0_0_0_0 default
1 /home/kolen/git/source/UCB-PHYS229-2019Spring-... 2_0_0_0_0_0_0 STELLAR_BARYON_FRAC=1.0
2 /home/kolen/git/source/UCB-PHYS229-2019Spring-... 0_1_0_0_0_0_0 STELLAR_BARYON_PL=-0.5
3 /home/kolen/git/source/UCB-PHYS229-2019Spring-... 0_2_0_0_0_0_0 STELLAR_BARYON_PL=1.0
4 /home/kolen/git/source/UCB-PHYS229-2019Spring-... 0_0_1_0_0_0_0 ESC_FRAC=0.001
5 /home/kolen/git/source/UCB-PHYS229-2019Spring-... 0_0_0_1_0_0_0 ESC_PL=-1.0
6 /home/kolen/git/source/UCB-PHYS229-2019Spring-... 0_0_0_2_0_0_0 ESC_PL=0.5
7 /home/kolen/git/source/UCB-PHYS229-2019Spring-... 0_0_0_0_1_0_0 M_TURNOVER=1e+08
8 /home/kolen/git/source/UCB-PHYS229-2019Spring-... 0_0_0_0_2_0_0 M_TURNOVER=1e+10
9 /home/kolen/git/source/UCB-PHYS229-2019Spring-... 1_0_0_0_0_0_0 STELLAR_BARYON_FRAC=0.001
10 /home/kolen/git/source/UCB-PHYS229-2019Spring-... 0_0_0_0_0_2_0 t_STAR=1.0
11 /home/kolen/git/source/UCB-PHYS229-2019Spring-... 0_0_0_0_0_0_1 L_X=38.0
12 /home/kolen/git/source/UCB-PHYS229-2019Spring-... 0_0_0_0_0_0_2 L_X=42.0

Class result from command line

In [20]:
df = pd.concat(
    (read_class_txt(path, camb=True) for path in df_path.path),
    axis=1,
    keys=df_path.name
#     keys=df_path.case
)
In [21]:
df.columns.names = ('case', 'spectra')
In [22]:
plot_column_slider(df, slider=True)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-22-7dbc5ba04638> in <module>()
----> 1 plot_column_slider(df, slider=True)

~/git/source/dautil-py/dautil/plot.py in plot_column_slider(df, chart, slider, imag_label)
    318         {df.columns.name: df.columns.values}
    319     if slider:
--> 320         values = {name: range(len(value)) for name, value in values}
    321 
    322     return dmap.redim.values(**values)

~/git/source/dautil-py/dautil/plot.py in <dictcomp>(.0)
    318         {df.columns.name: df.columns.values}
    319     if slider:
--> 320         values = {name: range(len(value)) for name, value in values}
    321 
    322     return dmap.redim.values(**values)

ValueError: too many values to unpack (expected 2)
In [ ]:
for spectrum in df.columns.levels[1]:
    py.plot(iplot_column_slider(df.T.loc[IDX[:, spectrum], :].T), filename=f'../docs/media/{spectrum}.html')
In [ ]:
for spectrum in df.columns.levels[1]:
    print(f'![{spectrum}](media/{spectrum}.html){{width=100% height=500}}', end='\n\n')